<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Specification extends CI_Controller {

  public function index()
	{		
		 $d['hello'] ="data";		 
		 $views['_content'] = "specification";
		 $sku = $this->uri->segment(2,0);  
  		 /*  Manipulate Curl Response   */ 
		   
		// echo $str die();
		 	
		 $d['prod'] =  $this->fetchShopSku($sku);
  	       
   	
		 $str = preg_replace("/Array|=>/   " ,"", $d['prod']); 
		 $str = str_replace( array('(',')','[',']') , '', $str);
		 $str = ltrim($str); $str= rtrim($str,'\t'); 
	 	 $lines = explode(PHP_EOL, $str);
		// var_dump($lines);
		$resp=array();
		 foreach( $lines as $k => $v){			
			if(  empty($v) == false ){		  			
				$v = preg_replace('/\s\s+/', ' ', $v);			
				//array_push($resp, trim($v));					
				array_push($resp, substr(strstr($v," "), 1));	
			}			
		 } 
	var_dump($resp);
		$a = array();
 		foreach($resp as $k => $v){
		
			if( preg_match("/sku/",$v) || preg_match("/imageurl/",$v) || preg_match("/thumbnail/",$v) || preg_match("/Dimension/",$v)  || preg_match("/Weight/",$v)  || preg_match("/Display/",$v)  || preg_match("/Card-Slot/",$v)  || preg_match("/Camera/",$v)  || preg_match("/Secondary-Camera/",$v)  || preg_match("/Java/",$v)
			    || preg_match("/Connectivity/",$v) || preg_match("/OS/",$v)  || preg_match("/ CPU/",$v) || preg_match("/name/",$v)  || preg_match("/description/",$v)  || preg_match("/shortdesciption/",$v) || preg_match("/size/",$v) || preg_match("/Talk-Time/",$v) || preg_match("/Stand-By-Time/",$v)	|| preg_match("/Stand-By-Time/",$v)	
			    || preg_match("/Internal/",$v) || preg_match("/Stand-By-Time/",$v) || preg_match("/Stand-By-Time/",$v)		)
			 {
				$temp = explode(" ", $v); 
				$key = $temp[0];$a[$key]="";
				foreach($temp as $ky => $vl ){						
					if($ky >= 1 ){
					 	$a[$key] .= $vl." ";
					}
				}				
			}
			else {
				 $rv = strrev($v);
				 $temp = explode(" ", $v); 
				 $key="";$a[$key]="";
				 foreach($temp as $ky => $vl ){						
					if($ky >= 1 ){
					 	$key .= $vl." ";
					}
				 }		
		
				 
			}	
			// loudspeaker pangit 
			if( preg_match("/".preg_quote("Loudspeaker/3.5mm","/")."/",$v) ){
				$temp = explode("Loudspeaker/3.5mm jack" , $v);
				$a["Loudspeaker/3.5mm jack"] = "";
				foreach($temp as $ky => $vl ){						
					if($ky >= 1 ){
					 	$a["Loudspeaker/3.5mm jack"].= $vl." ";
					}
				}	
			}
							
		} 
		var_dump($a);
		 /*  Manipulate Curl Response   */ 

		 //$this->template->load('template_landing', $views, $d);
	}
		
  public function fetchShopSku($sku){	    
     $ch = curl_init("http://50.18.174.131/maitai?".$sku);    // initialize curl handle
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     return curl_exec($ch);      
	
   } 
}
 